﻿/*
 
Celerity: Sensory Overload
Copright 2012 by Adam Hill, David Gannon, Thomas Tamblyn & Patrick Ytting
Contact: adam.hill@outlook.com
Website: adamhill.org
Licence: MS-Pl Licence
  
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Celerity.World.Particles
{
	public class ExplosionParticle : ParticleBase
	{
		public Vector3 velocity;
		public Vector3 acceleration;
		public float size;
		public float mass;
		public float airResistance;
		private float initialVelocityMag;
		private float velocityMag;
		private bool updated = false;
		private Color initialColour;

		public override void Update(GameTime gameTime, float deltaPhase)
		{
			if (!updated)
			{
				initialVelocityMag = (float)Math.Sqrt(velocity.X * velocity.X + velocity.Y * velocity.Y + velocity.Z * velocity.Z);
				initialColour = this.colour;
				updated = true;
			}
			Vector3 friction =  velocity * -airResistance;
			float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
			acceleration = friction / mass;
			Vector3 newVelocity = velocity + acceleration * time;
			postion += velocity * time + 0.5f * (acceleration * (time * time));
			velocity = newVelocity;
			postion.Z += deltaPhase;

			velocityMag = (float)Math.Sqrt(velocity.X * velocity.X + velocity.Y * velocity.Y + velocity.Z * velocity.Z);
			if(velocityMag < 0.001f) isAlive = false;

			float fadeFactor = velocityMag / initialVelocityMag;
			colour.A = (byte)((float)initialColour.A * (float)Math.Pow(fadeFactor, 0.3f));
		}
	}
}
